#pragma once
#include <iostream>
#include <string>
#include <strings.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <functional>
#include <unordered_map>

using namespace std;

using func_t = function<string(const string&, string&, uint16_t)>;

uint16_t default_port = 8080;
string defaultip = "0.0.0.0";
const int SIZE = 1024;

class UdpServer
{
public:
    UdpServer(uint16_t port = default_port, string ip = defaultip, bool isrunning = false) 
        : _port(port)
        , _ip(ip)
        , _isrunning(isrunning)
    {}

    void Init()
    {
        _sockfd = socket(AF_INET, SOCK_DGRAM, 0);
        if(_sockfd < 0)
        {
            cout << "create socket fail" << endl;
            exit(-1);
        }
        cout << _sockfd << endl;
        // bind socket
        struct sockaddr_in local;
        bzero(&local, sizeof(local));
        local.sin_family = AF_INET;
        local.sin_port = htons(_port);
        local.sin_addr.s_addr = inet_addr(_ip.c_str());

        int n = bind(_sockfd, (const sockaddr*)&local, sizeof(local));
        if(n < 0)
        {
            cout << "bind fail" << endl;
            exit(-1);
        }
        cout << "bind sucess" << endl;
    }

    void CheckUser(sockaddr_in& client)
    {
        string cip = inet_ntoa(client.sin_addr);
        uint16_t cport = client.sin_port;
        if(!_online_user.count(cip))
        {
            _online_user[cip] = client;
            cout << "[" + cip + ":" + to_string(cport) + "]# Add To Online Success" << endl;
        }
    }

    void Boardcast(const string& info, const string& ip, uint16_t port)
    {
        for(auto& user : _online_user)
        {
            string message = "[" + ip + ":" + to_string(port) + "]# " + info;

            socklen_t len = sizeof(user.second);
            sendto(_sockfd, message.c_str(),message.size(), 0, (sockaddr*)&user.second, len);
        }

    }

    void Run(func_t func)
    {
        _isrunning = true;
        char inbuffer[SIZE];
        
        while(_isrunning)
        {
            struct sockaddr_in client;
            socklen_t len = sizeof(client);
            
            // 接收
            ssize_t n = recvfrom(_sockfd, inbuffer, sizeof(inbuffer)-1, 0, (struct sockaddr*)&client, &len);    
            if(n < 0)
            {
                cout << "recvfrom error" << strerror(errno) << endl;
                exit(errno);
            }
            //拿到客户端的ip和port
            string cip = inet_ntoa(client.sin_addr);
            uint16_t cport = client.sin_port;

            // 查看用户
            CheckUser(client);
            
            // 回调func
            inbuffer[n] = 0;
            string info = inbuffer;
            string echo_string = func(info, cip, cport);
            Boardcast(info, cip, cport);

            // sendto(_sockfd, echo_string.c_str(), echo_string.size(), 0, (struct sockaddr*)&client, len);
        }
        
    }

    ~UdpServer()
    {
        if(_sockfd > 0)
        {
            close(_sockfd);
        }
    }
private:
    int _sockfd;
    uint16_t _port;
    string _ip;
    bool _isrunning;
    unordered_map<string, sockaddr_in> _online_user;
};